package com.peng.leetcode.tree;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;

/**
 * MaxDepth2
 * 559. N 叉树的最大深度
 * https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/
 * <p>
 * Created on 2023/1/27
 *
 * @author lupeng
 */
public class MaxDepth2 {

    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };

    public int maxDepth(Node root) {
        int ans = 0;
        if (root == null) {
            return ans;
        }
        Queue<Node> q = new ArrayDeque<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            ans++;
            for (int i = 0; i < size; i++) {
                Node n = q.poll();
                List<Node> children = n.children;
                if (children != null && children.size() > 0) {
                    for (Node child : children) {
                        q.offer(child);
                    }
                }
            }
        }
        return ans;
    }
}
